package com.bysj.lms.controller;


import com.bysj.lms.common.ResultModel;
import com.bysj.lms.pojo.entity.Role;
import com.bysj.lms.pojo.vo.RoleVo;
import com.bysj.lms.service.IRoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    private final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    IRoleService roleService;

    @ApiOperation(value = "增加角色信息")
    @PostMapping("/insert")
    public ResultModel insert(@RequestBody RoleVo roleVo){
        logger.info("角色信息：" + roleVo.toString());

        boolean save = roleService.save(new Role(roleVo));
        if(save){
            return ResultModel.success("新增角色成功");
        }else{
            return ResultModel.failure("新增角色失败");
        }
    }

    @ApiOperation(value = "查找角色",notes = "name为空查询所有角色，不为空模糊查询")
    @GetMapping("/find")
    public ResultModel findByPage(String name,Integer page,Integer count){
        PageInfo pageInfo = roleService.findByPage(name, page, count);
        return ResultModel.success("角色查询",pageInfo);
    }

    @ApiOperation(value = "根据id编辑角色信息")
    @PutMapping("/update")
    public ResultModel update(@RequestBody Role role){
        boolean update = roleService.updateById(role);
        if(update){
            return ResultModel.success("编辑成功");
        }
        return ResultModel.failure("编辑失败");
    }

    @ApiOperation(value = "根据id删除角色")
    @DeleteMapping("/delete")
    public ResultModel delete(Integer id){
        boolean delete = roleService.removeById(id);
        if(delete){
            return ResultModel.success("删除成功");
        }
        return ResultModel.failure("删除失败");
    }
}

